package com.slz3.business.module.systemManagement.sysPermissions;

import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.util.poi.ExcelUtils;
import com.slz3.business.module.systemManagement.sysPermissions.domain.SysPermissions;
import com.slz3.business.module.systemManagement.sysPermissions.steward.SysPermissionsCreateRow;
import com.slz3.business.module.systemManagement.sysPermissions.steward.SysPermissionsResponse;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
@Order(0)
public class SysPermissionsHandler {

    public Mono<ServerResponse> add(ServerRequest req) {
        return SysPermissionsResponse.add(req);
    }

    public Mono<ServerResponse> delete(ServerRequest req) {
        return SysPermissionsResponse.delete(req);
    }

    public Mono<ServerResponse> modify(ServerRequest req) {
        return SysPermissionsResponse.modify(req);
    }

    public Mono<ServerResponse> list(ServerRequest req) {
        return SysPermissionsResponse.list(req);
    }

    public Mono<ServerResponse> info(ServerRequest req) {
        return SysPermissionsResponse.info(req);
    }

    public Mono<ServerResponse> choice(ServerRequest req) {
        return SysPermissionsResponse.choice(req);
    }

    public Mono<ServerResponse> auditSubmit(ServerRequest req) {
        return SysPermissionsResponse.auditSubmit(req);
    }

    public Mono<ServerResponse> audit(ServerRequest req) {
        return SysPermissionsResponse.audit(req);
    }

    public Mono<ServerResponse> auditReject(ServerRequest req) {
        return SysPermissionsResponse.auditReject(req);
    }

    public Mono<ServerResponse> usingEnable(ServerRequest req) {
        return SysPermissionsResponse.usingEnable(req);
    }

    public Mono<ServerResponse> usingDisable(ServerRequest req) {
        return SysPermissionsResponse.usingDisable(req);
    }

    public Mono<ServerResponse> export(ServerRequest req)  {
        List<SysPermissions> all = Repositorys.sysPermissions.findAll();
        DataBuffer dataBuffer = ExcelUtils.createWorkBook2DataBuffer("test", all,
                new String[]{"uuid", "validFlag", "serAudit", "serMemo", "serNum", "serUsing", "serUri"},
                SysPermissionsCreateRow.createRow);
        return ServerResponse.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=demo.xlsx")
                .header("Accept-Ranges", "bytes")
                .contentType(MediaType.valueOf("application/vnd.ms-excel;charset=utf-8"))
                .contentLength(dataBuffer.capacity())
                .body(Mono.just(dataBuffer), DataBuffer.class);

    }

}
